﻿#include "QtNetSafeToolWindow.h"
#include "SARibbonApplicationButton.h"
#include "SARibbonBar.h"
#include "SARibbonButtonGroupWidget.h"
#include "SARibbonCategory.h"
#include "SARibbonCheckBox.h"
#include "SARibbonColorToolButton.h"
#include "SARibbonComboBox.h"
#include "SARibbonCustomizeDialog.h"
#include "SARibbonCustomizeWidget.h"
#include "SARibbonGallery.h"
#include "SARibbonLineEdit.h"
#include "SARibbonMenu.h"
#include "SARibbonPannel.h"
#include "SARibbonQuickAccessBar.h"
#include "SARibbonToolButton.h"
#include "SARibbonSystemButtonBar.h"
#include <QAbstractButton>
#include <QAction>
#include <QApplication>
#include <QButtonGroup>
#include <QCalendarWidget>
#include <QDebug>
#include <QElapsedTimer>
#include <QFile>
#include <QFileDialog>
#include <QFontComboBox>
#include <QLabel>
#include <QMenu>
#include <QMessageBox>
#include <QPushButton>
#include <QRadioButton>
#include <QSpinBox>
#include <QStatusBar>
#include <QTextEdit>
#include <QTextStream>
#include <QXmlStreamWriter>
#include <QMessageBox>
#include <QShortcut>
#include <QLineEdit>
#include <QDialogButtonBox>
#include <QSurfaceFormat>
#include <QVTKOpenGLNativeWidget.h>
#include <QSplitter.h>

#include "MyVtkViewWindow.h"
#include "DlgRunFindSubNets.h"




QtNetSafeToolWindow::QtNetSafeToolWindow(QWidget* parent)
    : SARibbonMainWindow(parent)
{
    InitUi();
}

QtNetSafeToolWindow::~QtNetSafeToolWindow()
{
}

void QtNetSafeToolWindow::InitUi()
{
    //设置窗口大小和位置
    int nWidth = 1200;
    int nHeight = 800;
    resize(nWidth, nHeight); // 调整窗口大小

    setWindowTitle(("QT Net Safe Tool"));
    setWindowIcon(QIcon("../icon/customize.png"));   //显示应用图标
    setStatusBar(new QStatusBar());

    //创建菜单栏
    {
        SARibbonBar* ribbon = ribbonBar();
        ribbon->setContentsMargins(5, 5, 5, 5);  //! 通过setContentsMargins设置ribbon四周的间距
        //ribbon->setRibbonStyle(SARibbonBar::RibbonStyleThreeRow);//设置  SARibbonBar::RibbonStyleCompactTwoRow

        CreateRibbonApplicationButton();  //显示应用程序按钮
        CreateQuickAccessBar();//创建快速访问栏
        CreateRightButtonGroup();//创建右侧按钮组示例

        SARibbonCategory* pCategoryModel = ribbon->addCategoryPage(tr("&NetSafe"));//创建主页
        pCategoryModel->setObjectName(("categoryNetSafe"));
        CreateCategoryMain(pCategoryModel);
    }

    //创建中心区域
    {
        auto centerWidget = new QWidget(this);
        centerWidget->setObjectName(("centerWidget"));
        centerWidget->setContentsMargins(5, 5, 5, 5);
        centerWidget->setStyleSheet("background-color:white;");

        QVBoxLayout* pCenterLayout = new QVBoxLayout(this);
        pCenterLayout->setContentsMargins(0, 0, 0, 0);
        pCenterLayout->setSpacing(1);

        {
            //创建qt splitter
            auto splitter = new QSplitter(this);
            splitter->setOrientation(Qt::Horizontal);
            splitter->setHandleWidth(10);
            splitter->setContentsMargins(5, 5, 5, 5);
            pCenterLayout->addWidget(splitter);

            //设置qtsplitter 设置比例
            splitter->setStretchFactor(0, 2);
            splitter->setStretchFactor(1, 6);
            splitter->setStretchFactor(2, 2);

            //创建qt leftTabWidget
            {
                auto lefTtabWidget = new QTabWidget(splitter);
                lefTtabWidget->setContentsMargins(0, 0, 0, 0);
                lefTtabWidget->setTabPosition(QTabWidget::South);
                lefTtabWidget->setDocumentMode(true);
                lefTtabWidget->setTabsClosable(false);
                lefTtabWidget->setMovable(false);
                lefTtabWidget->setTabBarAutoHide(false);
                lefTtabWidget->setTabShape(QTabWidget::Triangular);
                lefTtabWidget->setElideMode(Qt::ElideRight);
                lefTtabWidget->setUsesScrollButtons(true);
                int nLeft = nWidth / 6;
                lefTtabWidget->setMinimumWidth(nLeft);
                splitter->addWidget(lefTtabWidget);
            }

            {
                auto centerViewWidget = new QWidget(splitter);
                centerViewWidget->setObjectName(("centerViewWidget"));
                centerViewWidget->setContentsMargins(5, 5, 5, 5);
                centerViewWidget->setStyleSheet("background-color:white;");
                splitter->addWidget(centerViewWidget);

                QVBoxLayout* pCenterViewLayout = new QVBoxLayout(splitter);
                pCenterViewLayout->setContentsMargins(0, 0, 0, 0);
                pCenterViewLayout->setSpacing(5);

                {
                    auto viewWidget = CreateCenterViewWidget(centerViewWidget);
                    pCenterViewLayout->addWidget(viewWidget);
                }

                //创建qt buttomTtabWidget
                {
                    auto buttomTtabWidget = new QTabWidget(centerViewWidget);
                    buttomTtabWidget->setContentsMargins(0, 0, 0, 0);
                    buttomTtabWidget->setTabPosition(QTabWidget::South);
                    buttomTtabWidget->setDocumentMode(true);
                    buttomTtabWidget->setTabsClosable(false);
                    buttomTtabWidget->setMovable(false);
                    buttomTtabWidget->setTabBarAutoHide(false);
                    buttomTtabWidget->setTabShape(QTabWidget::Triangular);
                    buttomTtabWidget->setElideMode(Qt::ElideRight);
                    buttomTtabWidget->setUsesScrollButtons(true);
                    int nBottom = nHeight / 5;
                    buttomTtabWidget->setMaximumHeight(nBottom);
                    pCenterViewLayout->addWidget(buttomTtabWidget);
                }

                centerViewWidget->setLayout(pCenterViewLayout);
            }

            //创建qt rightTabWidget
            {
                auto rightTabWidget = new QTabWidget(splitter);
                rightTabWidget->setContentsMargins(0, 0, 0, 0);
                rightTabWidget->setTabPosition(QTabWidget::South);
                rightTabWidget->setDocumentMode(true);
                rightTabWidget->setTabsClosable(false);
                rightTabWidget->setMovable(false);
                rightTabWidget->setTabBarAutoHide(false);
                rightTabWidget->setTabShape(QTabWidget::Triangular);
                rightTabWidget->setElideMode(Qt::ElideRight);
                rightTabWidget->setUsesScrollButtons(true);
                int nRight = nWidth / 6;
                rightTabWidget->setMinimumWidth(nRight);
                splitter->addWidget(rightTabWidget);
            }
        }

        centerWidget->setLayout(pCenterLayout);
        setCentralWidget(centerWidget);
    }
}

void QtNetSafeToolWindow::CreateRibbonApplicationButton()
{
    SARibbonBar* ribbon = ribbonBar();
    if(!ribbon)
    {
        return;
    }

    if(!m_pMenuApplicationBtn)
    {
        m_pMenuApplicationBtn = new SARibbonMenu(this);
        m_pMenuApplicationBtn->addAction(CreateAction("test1", "../icon/CreateIcon/My_Create_Point.png"));
        m_pMenuApplicationBtn->addAction(CreateAction("test2", "../icon/action2.svg"));
        m_pMenuApplicationBtn->addAction(CreateAction("test3", "../icon/action3.svg"));
        m_pMenuApplicationBtn->addAction(CreateAction("test4", "../icon/action4.svg"));

        m_pMenuApplicationBtn->setWindowIcon(QIcon("../icon/customize.png"));
        m_pMenuApplicationBtn->setWindowTitle(tr("Application"));
        m_pMenuApplicationBtn->setFixedWidth(200);
        m_pMenuApplicationBtn->setFixedHeight(200);
        m_pMenuApplicationBtn->setContentsMargins(5, 5, 5, 5);
        m_pMenuApplicationBtn->setStyleSheet("background-color:white;");
    }

    SARibbonApplicationButton* pBtn = new SARibbonApplicationButton(this);
    pBtn->setText(("  &File  "));  // 文字两边留有间距，好看一点
    pBtn->setMenu(m_pMenuApplicationBtn);
    ribbon->setApplicationButton(pBtn);
}

void QtNetSafeToolWindow::CreateCategoryMain(SARibbonCategory* page)
{
    CreateCategoryCreate(page);
    CreateCategoryView(page);
}

void QtNetSafeToolWindow::CreateCategoryCreate(SARibbonCategory* page)
{
    // 使用addPannel函数来创建SARibbonPannel，效果和new SARibbonPannel再addPannel一样
    SARibbonPannel* pPannelCreateGeometry = page->addPannel(tr("Tool"));
    pPannelCreateGeometry->setToolTip(tr("Net Safe Tool"));
    pPannelCreateGeometry->setObjectName(("pannelTool"));

    {
        QAction* pActionCreateVolume = CreateAction(tr("FindSubNets"), "../icon/CreateIcon/My_Create_Volume.png");
        pPannelCreateGeometry->addLargeAction(pActionCreateVolume);
        connect(pActionCreateVolume, &QAction::triggered, this, [=]()
                {
                    DlgRunFindSubNets tempDlg;
                    tempDlg.exec();
                });
    }
}

void QtNetSafeToolWindow::CreateCategoryView(SARibbonCategory* page)
{
    // 使用addPannel函数来创建SARibbonPannel，效果和new SARibbonPannel再addPannel一样
    SARibbonPannel* pPannelViewSet = page->addPannel(tr("View Set"));
    pPannelViewSet->setToolTip(tr("View Set"));
    pPannelViewSet->setObjectName(("pannelViewSet"));

    {
        QAction* pActionViewVertex = CreateAction(tr("Vertex"), "../icon/CreateIcon/My_View_Type_Vertex.png");
        pPannelViewSet->addLargeAction(pActionViewVertex);
        /*   connect(pActionViewVertex, &QAction::triggered, this, [=]()
           {
               MyVtkViewWindow::GetInstance()->SetViewType(MyVtkViewWindow::E_VIEW_TYPE_VERTEX);
           });*/
    }

    {
        QAction* pActionViewEdge = CreateAction(tr("Edge"), "../icon/CreateIcon/My_View_Type_Edge.png");
        pPannelViewSet->addLargeAction(pActionViewEdge);
        /* connect(pActionViewEdge, &QAction::triggered, this, [=]()
         {
             MyVtkViewWindow::GetInstance()->SetViewType(MyVtkViewWindow::E_VIEW_TYPE_EDGE);
         });*/
    }

    {
        QAction* pActionViewFace = CreateAction(tr("Face"), "../icon/CreateIcon/My_View_Type_Face.png");
        pPannelViewSet->addLargeAction(pActionViewFace);
        /*connect(pActionViewFace, &QAction::triggered, this, [=]()
        {
            MyVtkViewWindow::GetInstance()->SetViewType(MyVtkViewWindow::E_VIEW_TYPE_FACE);
        });*/
    }

}

void QtNetSafeToolWindow::CreateQuickAccessBar()
{
    SARibbonQuickAccessBar* quickAccessBar = ribbonBar()->quickAccessBar();

    quickAccessBar->addAction(CreateAction("Save", "../icon/save.svg", "save-quickbar"));
    quickAccessBar->addSeparator();

    QAction* actionUndo = CreateAction("Undo", "../icon/undo.svg");
    actionUndo->setShortcut(QKeySequence("Ctrl+Shift+z"));
    actionUndo->setShortcutContext(Qt::ApplicationShortcut);
    quickAccessBar->addAction(actionUndo);

    QAction* actionRedo = CreateAction("Redo", "../icon/redo.svg");
    actionRedo->setShortcut(QKeySequence("Ctrl+z"));
    actionRedo->setShortcutContext(Qt::ApplicationShortcut);
    quickAccessBar->addAction(actionRedo);
}

void QtNetSafeToolWindow::CreateRightButtonGroup()
{
    SARibbonBar* ribbon = ribbonBar();
    if(!ribbon)
    {
        return;
    }

    SARibbonButtonGroupWidget* rightBar = ribbon->rightButtonGroup();
    QAction* actionHelp = CreateAction(tr("help"), "../icon/help.svg");
    rightBar->addAction(actionHelp);
}

QAction* QtNetSafeToolWindow::CreateAction(const QString& text, const QString& iconurl, const QString& objName)
{
    QAction* act = new QAction(this);
    act->setText(text);
    act->setIcon(QIcon(iconurl));
    act->setObjectName(objName);
    return act;
}

QAction* QtNetSafeToolWindow::CreateAction(const QString& text, const QString& iconurl)
{
    QAction* act = new QAction(this);
    act->setText(text);
    act->setIcon(QIcon(iconurl));
    act->setObjectName(text);
    return act;
}

QWidget* QtNetSafeToolWindow::CreateCenterViewWidget(QWidget* parent)
{
    auto pViewWidget = MyVtkViewWindow::GetInstance(parent);
    return pViewWidget;
}





//// --------------MyProcess.cpp------------------------//
//MyProcess::MyProcess()
//{
//    this->Argc = 0;
//    this->Argv = nullptr;
//}
//
//void MyProcess::SetArgs(int anArgc, char* anArgv[])
//{
//    this->Argc = anArgc;
//    this->Argv = anArgv;
//}
//
//MyProcess* MyProcess::New()
//{
//    return new MyProcess();
//}
//
//void MyProcess::Execute()
//{
//    // multiprocess logic
//    int numProcs = this->Controller->GetNumberOfProcesses();
//    int me = this->Controller->GetLocalProcessId();
//
//    cout << "numProcs=" << numProcs << " me=" << me << endl;
//    cout << "executable=" << this->Argv[0] << endl;
//    cout << "argc=" << this->Argc << endl;
//
//    const int MY_RETURN_VALUE_MESSAGE = 0x11;
//
//    if(me == 0)
//    {
//        // root node
//        this->ReturnValue = 0;
//        int i = 1;
//        while(i < numProcs)
//        {
//            this->Controller->Send(&this->ReturnValue, 1, i, MY_RETURN_VALUE_MESSAGE);
//            ++i;
//        }
//    }
//    else
//    {
//        // satellites
//        this->Controller->Receive(&this->ReturnValue, 1, 0, MY_RETURN_VALUE_MESSAGE);
//    }
//}